#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <windows.h>

#define AUTO 1
#define SN_NUM 10

#define POLY 0x1021
#define WIDTH 16
#define INIT_CRC 0x0000 // ???????0x0000???????????????????


#define CALIBRATION_NUM  (3)//三个校准点
#define EN_AD_T_NUM      (6)//温度点数

const float pfSampleTemperature[CALIBRATION_NUM][EN_AD_T_NUM] = {
    {2994	,2996	,2996	,2997	,2993	,3515},
    {933	,934	,935	,935	,931	,1309},
    {357	,357	,358	,357	,356	,438}
};

typedef struct
{
  float pfCalibrationTemperature[CALIBRATION_NUM];
  float pfSampleTemperature[CALIBRATION_NUM][EN_AD_T_NUM];
  float fK[CALIBRATION_NUM - 1][EN_AD_T_NUM];
  float fB[CALIBRATION_NUM - 1][EN_AD_T_NUM];
} st_calibrationData_in_flash_t;

#pragma pack(1)
// sizeof(st_flash_data_t):256
typedef struct
{
  uint8_t ubHead;
  uint8_t ubFvtTestResult;
  uint16_t crc16;
  uint32_t uiSn;
  st_calibrationData_in_flash_t stCalibrationDataInFlash;
  float fre_offset;
  uint8_t pubRes2[60];
} st_flash_data_t;
#define SIZE_FLASH_DATA (sizeof(st_flash_data_t))
#pragma pack()

uint8_t reverse_byte(uint8_t b)
{
    b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
    b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
    b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
    return b;
}

uint16_t crc_ccitt(uint8_t *data_p, size_t length)
{
    uint16_t crc = INIT_CRC;
    uint8_t crcl, crch;
    size_t i, j;

    for (i = 0; i < length; ++i)
    {
        crc ^= (uint16_t)((reverse_byte(*data_p++)) << 8); // ????????CRC???8λ

        for (j = 0; j < 8; ++j)
        {
            if (crc & 0x8000)
            {                            // ??????λ
                crc = (crc << 1) ^ POLY; // ????????????
            }
            else
            {
                crc <<= 1; // ????
            }
        }
    }
    crcl = crc & 0x00FF;
    crch = crc >> 8;
    crcl = reverse_byte(crcl);
    crch = reverse_byte(crch);
    crc = (crcl << 8) | crch;
    return crc;
}

unsigned int get_file_size(char *filename)
{
    HANDLE hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        printf("Error opening file\n");
        return 1;
    }

    DWORD highDword = 0;
    DWORD lowDword = GetFileSize(hFile, &highDword);
    if (lowDword == INVALID_FILE_SIZE && GetLastError() != NO_ERROR)
    {
        printf("Error getting file size\n");
        CloseHandle(hFile);
        return 1;
    }

    // ????????С??????????λ??
    __int64 fileSize = ((__int64)highDword << 32) | lowDword;
    // printf("File size is %lld bytes\n", fileSize);

    CloseHandle(hFile);
    return (unsigned int)fileSize;
}

void GetLinearFittingSimple(float fX1, float fY1, float fX2, float fY2, float *pfK, float *pfB)
{
    if (fX1 == fX2)
    {
        return;
    }

    *pfK = (fY2 - fY1) / (fX2 - fX1);
    *pfB = fY1 - (*pfK) * fX1;
}

st_flash_data_t gstFlashData;



int main(int argc, char *argv[])
{
    // char in;
    // char sn_name[9];
    unsigned int sn, size;

    if (!argv[1])
    {
        return 0;
    }

    size = get_file_size(argv[1]);
    size %= 1024 * 32 + 1;
    if (size != 1024 * 32)
    {
        printf("数据大小不对！%d\n",size);
        system("pause");
        return 0;
    }
    int numRead;
    unsigned char buffer[1024 * 32];
    char name[8 + 13];
    FILE *file = fopen(argv[1], "rb");
    // FILE *file = fopen("10002FC6_CALIED.bin", "rb");
    if (file == NULL)
    {
        printf("File error1!\n");
    }
    numRead = (int)fread(buffer, 1, size, file);
    if (ferror(file))
    {
        printf("File error2!\n");
    }
    fclose(file);
    gstFlashData = *((st_flash_data_t *)&buffer[0X7C00]);
    memcpy(gstFlashData.stCalibrationDataInFlash.pfSampleTemperature,pfSampleTemperature,sizeof(pfSampleTemperature));
    gstFlashData.ubFvtTestResult = 0xFF;
    memcpy(&buffer[0X7C00],&gstFlashData,sizeof(st_flash_data_t));

    // 打开二进制文件用于写入 ("wb" 模式)  
    file = fopen("output.bin", "wb");  
    if (file == NULL) {  
        perror("Failed to open file");  
        return EXIT_FAILURE;  
    }  
    // 写入数据到文件  
    size_t dataSize = 1024 * 32;
    size_t written = fwrite(buffer, sizeof(unsigned char), dataSize, file);  
    if (written != dataSize) {  
        perror("Failed to write complete data to file");
        fclose(file);  
        return EXIT_FAILURE;  
    }  
    // 关闭文件  
    fclose(file);  

    (void)argc;
    (void)argv;
    return 0;
}